<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
     echo "<img src = \"calhead.jpg\" alt = \"Calendar!\" /><br>";
     //echo("Calendar!");

function generateCalendarList() {
    $calendarArray = array();
    $calendarArray = calendarManager::getCalendar($_SESSION['userID']); 

    $default = $default . "<ul>" ;
    //echo "<table border=\"1\"><tr>";
    foreach ($calendarArray as $calendar) {
        //$address = $link->getAddress();
        $eventName = $calendar->getEventName();
        $descript = $calendar->getDescript();
        $location = $calendar->getLocation();
        $time = $calendar->getTime();
        $date = $calendar->getDate();



        $default = $default . "<li><b>". $eventName . "</b>  ";
        if($descript != NULL)
            $default = $default . " : ";
        if($date != "0000-00-00")
            $default = $default .  $date . "  ";
        $default = $default . "<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $descript;
        if($location != NULL)
            $default = $default . " <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $location . "  ";
        if($time != "00:00:00")
            $default = $default . " <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ". $time . "  ";
        

        $default = $default . "</li>";
        
    }

    $default = $default . "</ul><a href=index.php?add=calendar><br>Add Calendar Event</a> | <a href=index.php?edit=calendar>Edit Calendar</a>";

    return $default;
}

function generateEditDeleteListCal() {
    $calendarArray = array();
    $calendarArray = calendarManager::getCalendar($_SESSION['userID']);
    foreach ($calendarArray as $calendar) {
        $default = $default 
             . $calendar->getEventName() . " <a href=\"index.php?edit=calendar&id="
             . $calendar->getCalendarID() . "\">Edit</a> <a href=\"index.php?delete=calendar&id="
             . $calendar->getCalendarID() . "\">Delete</a><br>";
    }

    return $default;
}

function generateFormCal($action) {

    if (isset($_GET['id']))
    {
        $calendar = calendarManager::getCalendarEvent($_GET['id']);
        
        $eventName = " value=\"" . $calendar->getEventName() . "\"";
        $date = " value=\"" . $calendar->getDate() . "\"";
        $time = " value=\"" . $calendar->getTime() . "\"";
        $descript = " value=\"" . $calendar->getDescript() . "\"";
        $Location = "value=\"" . $calendar->getLocation() . "\"";
        $AlertBool = "value=\"" . $calendar->getAlertBool() . "\"";


    } else
    {
        $eventName = "";
        $date = "";
        $time = "";
        $descript = "";
        $Location = "";
        $AlertBool = FALSE;
    }

    $form = "
        <form method=\"POST\" action=\"$action\"><table>
        <tr><td>*Event Name:</td><td><input type=\"text\" name=\"eventName\" $eventName></td></tr>
        <tr><td>Date of Event (MM/DD/YYYY):</td><td>
<select name=\"eventMonth\">
                        <option value=\"01\">January</option>
                        <option value=\"02\">February</option>
                        <option value=\"03\">March</option>
                        <option value=\"04\">April</option>
                        <option value=\"05\">May</option>
                        <option value=\"06\">June</option>
                        <option value=\"07\">July</option>
                        <option value=\"08\">August</option>
                        <option value=\"09\">September</option>
                        <option value=\"10\">October</option>
                        <option value=\"11\">November</option>
                        <option value=\"12\">December</option>
                    </select>
                    <select name=\"eventDay\">
                        <option value=\"01\">01</option>
                        <option value=\"02\">02</option>
                        <option value=\"03\">03</option>
                        <option value=\"04\">04</option>
                        <option value=\"05\">05</option>
                        <option value=\"06\">06</option>
                        <option value=\"07\">07</option>
                        <option value=\"08\">08</option>
                        <option value=\"09\">09</option>
                        <option value=\"10\">10</option>
                        <option value=\"11\">11</option>
                        <option value=\"12\">12</option>
                        <option value=\"13\">13</option>
                        <option value=\"14\">14</option>
                        <option value=\"15\">15</option>
                        <option value=\"16\">16</option>
                        <option value=\"17\">17</option>
                        <option value=\"18\">18</option>
                        <option value=\"19\">19</option>
                        <option value=\"20\">20</option>
                        <option value=\"21\">21</option>
                        <option value=\"22\">22</option>
                        <option value=\"23\">23</option>
                        <option value=\"24\">24</option>
                        <option value=\"25\">25</option>
                        <option value=\"26\">26</option>
                        <option value=\"27\">27</option>
                        <option value=\"28\">28</option>
                        <option value=\"29\">29</option>
                        <option value=\"30\">30</option>
                        <option value=\"31\">31</option>
                    </select>

<input type=\"text\" name=\"eventYear\" size=4 $eventYear />
</td></tr>
        <tr><td>Time of Event:</td><td><input type=\"text\" name=\"time\" $time> Enter time in format hh:mm</td></tr>
        <tr><td>Description:</td><td><input type=\"text\" name=\"descript\" $descript></td></tr>
        <tr><td>Location:</td><td><input type=\"text\" name=\"Location\" $Location></td></tr>
        <tr><td>Alert?</td><td><input type=\"checkbox\" name=\"AlertBool\" $AlertBool></td></tr>
        </table>
        <input type=\"submit\" value=\"Submit\">
        </form>
        ";

    return $form;
}

if ($add == "calendar")
{
    if ($submit == "1")
    {
        if (isset($_POST['eventName']))// && isset($_POST['date']) && isset($_POST['time']) && isset($_POST['descript']) && isset($_POST['Location']) && isset($_POST['AlertBool']))
        {
            $calendar = new Calendar();
            $calendar->setUserID($_SESSION['userID']);
            $calendar->setCalendarID($_GET['id']);
            $calendar->setEventName($_POST['eventName']);
            if($_POST['eventYear'] < 2011)
                echo "<br>INCORRECT YEAR<br>";
            $calendar->setDate($_POST['eventYear'] . "-" . $_POST['eventMonth'] . "-" . $_POST['eventDay']);
            $calendar->setTime($_POST['time']);
            $calendar->setDescript($_POST['descript']);
            $calendar->setAlertBool($_POST['AlertBool']);
            $calendar->setLocation($_POST['Location']);

            calendarManager::addCalendar($calendar);
            //echo "<br>";
            echo generateCalendarList();
        } else
        {
            echo "<br>Please complete the entire form.<br>";
            $action = "index.php?add=calendar&submit=1";
            echo generateFormCal($action);
        }
    } else
    {
        $action = "index.php?add=calendar&submit=1";
        echo generateFormCal($action);
    }
} else if ($edit == "calendar")
{
    if (isset($_GET['id']))
    {
        if ($submit == "1")
        {
            if (isset($_POST['eventName'])) //&& isset($_POST['date']) && isset($_POST['time']) && isset($_POST['descript']) && isset($_POST['Location']) && isset($_POST['AlertBool']))
            {
                
                $calendar = new Calendar();
                $calendar->setUserID($_SESSION['userID']);
                $calendar->setCalendarID($_GET['id']);
                $calendar->setEventName($_POST['eventName']);
                if($_POST['eventYear'] < 2011)
                    echo "<br>INCORRECT YEAR<br>";
                $calendar->setDate($_POST['eventYear'] . "-" . $_POST['eventMonth'] . "-" . $_POST['eventDay']);
                $calendar->setTime($_POST['time']);
                $calendar->setDescript($_POST['descript']);
                $calendar->setAlertBool($_POST['AlertBool']);
                $calendar->setLocation($_POST['Location']);
                
                calendarManager::editCalendar($calendar);
                echo generateCalendarList();

            } else
            {
                echo "Please complete the entire form.<br>";
                $action = "index.php?edit=calendar&id=". $_GET['id'] . "&submit=1";
                echo generateFormCal($action);
            }
        } else
        {
            $action = "index.php?edit=calendar&id=". $_GET['id'] . "&submit=1";
            echo generateFormCal($action);
        }
    } else
    {
        echo generateEditDeleteListCal();
    }

} else if ($delete == "calendar")
{
    if (isset($_GET['id']))
    {
        $calendar = calendarManager::getCalendarEvent($_GET['id']);
        $eventName = $calendar->getEventName();
        $date = $calendar->getDate();
        $time = $calendar->getTime();
        $descript = $calendar->getDescript();
        $Location = $calendar->getLocation();
        $AlertBool = $calendar->getAlertBool();
        
        if ($submit == "1")
        {
            calendarManager::deleteCalendar($calendar);
            echo generateCalendarList();
        } else
        {
            echo "Are you sure you want to delete this calendar Event?<br><br>$eventName<br><br>
            <a href=index.php?delete=calendar&id=" . $_GET['id'] . "&submit=1>Yes</a> | <a href=index.php>No</a>";
        }
    } else
    {
        echo "You must select a calendar event to delete.";
        echo generateEditDeleteListCal();
    }
} else
{
   // echo "<br>";
    echo generateCalendarList();
}

     

?>
